package tree.recurrence;

import tree.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName PostOrder
 * @Description TODO
 * @Author lenovo
 * @Date 2023-03-07 10:54
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class PostOrder {

    private List<Integer> PostOrderTran(TreeNode treeNode) {

        List<Integer> res = new ArrayList<>();

        postOrder(treeNode, res);

        return res;
    }

    public void postOrder(TreeNode treeNode, List<Integer> res) {
        if (treeNode == null) {
            return;
        }
        postOrder(treeNode.left, res);
        postOrder(treeNode.right, res);
        res.add(treeNode.val);
    }

    public static void main(String[] args) {
        PostOrder postOrder = new PostOrder();

        TreeNode treeNode = new TreeNode(2);

        TreeNode treeNodeLeft = new TreeNode(1);
        treeNode.left = treeNodeLeft;

        TreeNode treeNodeRight = new TreeNode(3);
        treeNode.right = treeNodeRight;


        List<Integer> list = postOrder.PostOrderTran(treeNode);

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}